package cn.atzxc.qy.service.impl;

import cn.atzxc.qy.domain.Address;
import cn.atzxc.qy.domain.Rider;
import cn.atzxc.qy.mapper.RiderMapper;
import cn.atzxc.qy.service.RiderService;
import cn.atzxc.qy.utils.JwtUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.jsonwebtoken.Claims;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.ImportAutoConfiguration;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

@Service
public class RiderServiceImpl implements RiderService {

    @Autowired
    private RiderMapper riderMapper;

    /**
     * 用户已经认证，查询出该登录用户的认证信息
     * 通过token解析出用户的ID，然后从骑手表中查询出认证信息
     * @param token
     * @return
     * @throws Exception
     */
    @Override
    public Rider getRiderInfo(String token) throws Exception {
        //通过token解析出指定的用户名ID
        Claims claims = JwtUtil.parseJWT(token);
        String subject = claims.getSubject();
        System.out.println("解析出的ID为："+subject);
        LambdaQueryWrapper<Rider> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Rider::getUserId, subject);
        Rider rider = riderMapper.selectOne(lambdaQueryWrapper);
        return rider;
    }

    /**
     * 用户提交认证，点击提交认证后，变为审核中
     * @param rider
     * @return
     */
    @Override
    public int submitRider(String token,Rider rider) throws Exception {
        String userId = JwtUtil.getUserIdFromToken(token);
        rider.setUserId(Long.parseLong(userId));//设置userid
        rider.setScores(90);
        rider.setRegistrationDate(new Timestamp(new Date().getTime()));
        rider.setAuthStatus("审核中");

        String stuURL = strToJSON(rider.getIdentificationStuImageUrl());
        rider.setIdentificationStuImageUrl(stuURL);
        String cardURL = strToJSON(rider.getIdentificationCardImageUrl());
        rider.setIdentificationCardImageUrl(cardURL);

        System.out.println("骑手信息："+rider);
        int insert = riderMapper.insert(rider);
        return insert;
    }

    /**
     * 获取用户认证状态
     * 1.通过token解析出用户ID
     * 2.根据用户ID查询出指定骑手表的认证状态
     * @param token
     * @return
     */
    @Override
    public Rider getUserAuthState(String token) throws Exception {
        String userId = JwtUtil.getUserIdFromToken(token);
        //根据用户ID查询出骑手表的认证状态
        LambdaQueryWrapper<Rider> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Rider::getUserId, userId);
        Rider rider = riderMapper.selectOne(lambdaQueryWrapper);
        return rider;
    }

    @Override
    public int resubmit(String token, Rider rider) throws Exception {
        String userId = JwtUtil.getUserIdFromToken(token);
        rider.setUserId(Long.parseLong(userId));//设置userid
        rider.setScores(90);
        rider.setRegistrationDate(new Timestamp(new Date().getTime()));
        rider.setAuthStatus("审核中");

        String stuURL = strToJSON(rider.getIdentificationStuImageUrl());
        rider.setIdentificationStuImageUrl(stuURL);
        String cardURL = strToJSON(rider.getIdentificationCardImageUrl());
        rider.setIdentificationCardImageUrl(cardURL);

        System.out.println("重新提交骑手信息："+rider);
        LambdaUpdateWrapper<Rider> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(Rider::getUserId, userId); // 设置更新条件，假设这里是 userId

        int update = riderMapper.update(rider, updateWrapper);
        return update;
    }


    public String strToJSON(String str) throws JsonProcessingException {
        //String str = "https://atzxc-blog.oss-cn-hangzhou.aliyuncs.com/riderAuthImages/2024/03/01/1709259599571.png,https://atzxc-blog.oss-cn-hangzhou.aliyuncs.com/riderAuthImages/2024/03/01/1709259602806.png";
        // 将字符串解析为 List 或数组
        List<String> list = Arrays.asList(str.split(","));
        // 创建 ObjectMapper 实例
        ObjectMapper mapper = new ObjectMapper();
        // 将 List 转换为 JSON 格式的字符串
        String json = mapper.writeValueAsString(list);
        System.out.println(json); // 输出 ["value1","value2","value3"]
        return json;
    }






















}
